package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.domain.Setmeal;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckItemService;

import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 体检套餐管理
 *
 */


@RestController
@RequestMapping("/setmeal")
public class SetmealContorller {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){

        return setmealService.findAll(queryPageBean);
    }

    /**
     * 图片上传
     * @param imgFile
     * @return
     */
    @RequestMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){

        //获得上传图片的真实名称
        String originalFilename = imgFile.getOriginalFilename();
        //截取后缀名
        int index = originalFilename.lastIndexOf(".");
        String s = originalFilename.substring(index - 1);
        //生成随机名称并加上后缀名
        String fileName = UUID.randomUUID().toString() + s;

        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //将上传的图片名称存入redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加检查套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal ,Integer[] checkgroupIds){

        try {
            setmealService.add(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<Setmeal> all = setmealService.findAll();
            return new Result(true,"获取套餐列表成功",all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取套餐列表失败");
        }
    }

        @RequestMapping("findAllAddress")
    public Result findAllAddress(){
        try {
            List<Map<String,Object>> all = setmealService.findAllAddress();
            return new Result(true,"获取地址成功",all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取地址失败");
        }
    }
}
